utils = {}

function utils.AddVariable(vindex,vname,vlable,vtype)
    local Variable = {
        Index = vindex,
        Name  = vname,
        Label = vlable,
        --Value = "",
        Type  = vtype,
    }
    --print(variable)
    return Variable
end

function utils.AppendVariable(vindex,vname,vValue,vExplain)
    local Variable = {
        Index = vindex,
        Name  = vname,
        Label = "",
        Value = vValue,
        Explain = vExplain,
        Type  = "",
    }
    --print(variable)
    return Variable
end

function utils.And(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local ret = 0
    local count = 0
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 and s1 == 1 then
            ret = ret + 2^count
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
        count = count + 1
    until(tmp1 == 0 and tmp2 == 0)
    return ret
end

function utils.Not(num,len)
    local tmp1 = num
    local ret = 0
    local count = 0
    for index=1,len,1 do
        local s1 = math.modf(tmp1 % 2)
        if s1 == 0 then
            ret = ret + 2^count
        end
        tmp1 = math.modf(tmp1/2)
        count = count + 1
    end
    return ret
end

function utils.Hex2Str(buf,bufLen)
    local ret = {}
    local low
    local high
    --local str 

    for index=1,bufLen,1 do
        if(math.modf(buf[index]/16) >= 0x0A) then
            high = math.modf(buf[index]/16) + 0x37
        elseif(math.modf(buf[index]/16) < 0x0A) then
            high = math.modf(buf[index]/16) + 0x30    
        end

        if(math.modf(buf[index]%16) >= 0x0A) then
            low = math.modf(buf[index]%16) + 0x37
        elseif(math.modf(buf[index]%16) < 0x0A) then
            low = math.modf(buf[index]%16) + 0x30    
        end

        ret[2*index-1] = high
        ret[2*index] = low
        --str = string.format("%02X",ret[2*index-1])
        --print(str)
        --str = string.format("%02X",ret[2*index])
        --print(str)
        index = index + 1
    end

    return ret
end

function utils.Str2Hex(buf,bufLen)
    
    local ret = {}
    local high = 0
    local low = 0
    --local str
    --数据长度必须是偶数
    if(math.modf(bufLen%2) == 0)
    then
        for index=1,bufLen/2,1 do
            if(buf[2*index-1]>=0x30 and buf[2*index-1]<=0x39) then
                high = math.modf((buf[2*index-1]-0x30)*16)
            elseif(buf[2*index-1]>=0x41 and buf[2*index-1]<=0x5A)  then
                high = math.modf((buf[2*index-1]-0x37)*16)
            end

            if(buf[2*index]>=0x30 and buf[2*index]<=0x39) then
                low = math.modf((buf[2*index]-0x30))
            elseif(buf[2*index]>=0x41 and buf[2*index]<=0x5A)  then
                low = math.modf((buf[2*index]-0x37))
            end

            ret[index] = high+low
            --str = string.format("%02X",ret[index])
            --print(str)
            index = index + 1
        end
    end

    return ret
end

function utils.Copy(buf,index,bufLen)
    local tmp = {}

    for i=0,bufLen,1 do
        table.insert(tmp,buf[i+index])
    end
    
    return tmp
end

--local rxBuf = {0x22,0x01,0x4A,0x42,0xE0,0x02,0x01,0xFD,0x28}
--utils.Hex2Str(rxBuf,9)

--local rxBuf = {0x32,0x32,0x30,0x31,0x34,0x41,0x34,0x32,0x45,0x30,0x30,0x32,0x30,0x31,0x46,0x44,0x32,0x38}
--utils.Str2Hex(rxBuf,18)

function utils.GetCHKSUM(buf,buflen)
    local sum = 0

    --求和
    for index=1,buflen,1 do
        sum = sum + buf[index]
    end

    --累加值对65536取模，取反加1
    local chksum = utils.Not(math.modf(sum%65536),16)+1

    --换算成2个字节hex
    local chksumTable = {}
    local tmp = math.modf(chksum/256)
    table.insert(chksumTable,tmp)
    tmp = math.modf(chksum%256)
    table.insert(chksumTable,tmp)

    --换算成4个字节ascii
    local chksumStr = {}
    chksumStr = utils.Hex2Str(chksumTable,2)

    return chksumStr
end

function utils.GetLENGTH(length)

    local tmp = length[1]*256+length[2]

    local low = utils.And(tmp,0x0FFF) - 1
    local len = utils.And(utils.Not(low,16),0x0FFF)
    len = math.modf(len%65536)

    print("len "..string.format("%04X",len))
    
end

function utils.test()
    local rxBuf = {0x22,0x01,0x4A,0x42,0xE0,0x02,0x01}

    local str = utils.Hex2Str(rxBuf,7)
    local chksum = utils.GetCHKSUM(str,14)

    local adu = {}
    local aduLen = 0
    table.insert(adu,0x7E)
    aduLen = aduLen + 1
    for index=1,14,1 do
        table.insert(adu,str[index])
    end
    aduLen = aduLen + 14
    for index=1,4,1 do
        table.insert(adu,chksum[index])
    end
    aduLen = aduLen + 4
    table.insert(adu,0x0D)
    aduLen = aduLen + 1

    local tmp = ""
    for index=1,aduLen,1 do
        tmp = tmp .. " " .. string.format("%02X",adu[index])
    end
    print(tmp)
end

--utils.test()

function utils.AnalysisRx(sAddr,rxBufCnt)

    local rxBuf = {
         0x7E ,0x32 ,0x32 ,0x30 ,0x31 ,0x34 ,0x41 ,0x30 ,0x30 ,0x41 ,0x30
        ,0x41 ,0x43 ,0x30 ,0x31 ,0x30 ,0x30 ,0x33 ,0x45 ,0x31 ,0x34 ,0x39 ,0x38 ,0x31 ,0x30 ,0x30
        ,0x43 ,0x44 ,0x46 ,0x30 ,0x43 ,0x45 ,0x30 ,0x30 ,0x43 ,0x44 ,0x46 ,0x30 ,0x43 ,0x45 ,0x31 
        ,0x30 ,0x43 ,0x44 ,0x46 ,0x30 ,0x43 ,0x45 ,0x30 ,0x30 ,0x43 ,0x45 ,0x30 ,0x30 ,0x43 ,0x45 
        ,0x31 ,0x30 ,0x43 ,0x44 ,0x45 ,0x30 ,0x43 ,0x44 ,0x44 ,0x30 ,0x43 ,0x44 ,0x46 ,0x30 ,0x43 
        ,0x44 ,0x46 ,0x30 ,0x43 ,0x45 ,0x30 ,0x30 ,0x43 ,0x44 ,0x46 ,0x30 ,0x43 ,0x44 ,0x46 ,0x30 
        ,0x43 ,0x45 ,0x30 ,0x30 ,0x30 ,0x46 ,0x30 ,0x30 ,0x30 ,0x44 ,0x43 ,0x30 ,0x30 ,0x45 ,0x36 
        ,0x30 ,0x34 ,0x30 ,0x30 ,0x44 ,0x43 ,0x30 ,0x30 ,0x44 ,0x43 ,0x30 ,0x30 ,0x44 ,0x43 ,0x30 
        ,0x30 ,0x44 ,0x43 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x44 ,0x30 ,0x44 ,0x30 ,0x30 ,0x33 ,0x45 
        ,0x30 ,0x44 ,0x32 ,0x34 ,0x41 ,0x34 ,0x31 ,0x36 ,0x42 ,0x37 ,0x30 ,0x30 ,0x31 ,0x36 ,0x30 
        ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 
        ,0x30 ,0x38 ,0x32 ,0x33 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 
        ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x44 ,0x38 ,0x32 ,0x41 ,0x0D}

    local SOI,VER,ADR,CID1,CID2,LENGTHTmp,LENGTH,COMMAND,DATA,CHKSUM,EOI

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    if(rxBufCnt < 11)
    then
        rxBuf = {}
        return VariableMap
    end

    local tmp = {}
    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            SOI  = rxBuf[index]
            if(SOI == 0x7E) then    
                VER  = utils.Str2Hex(utils.Copy(rxBuf,index+1,2),2)
                ADR  = utils.Str2Hex(utils.Copy(rxBuf,index+3,2),2)
                CID1 = utils.Str2Hex(utils.Copy(rxBuf,index+5,2),2)
                CID2 = utils.Str2Hex(utils.Copy(rxBuf,index+7,2),2)
                LENGTHTmp = utils.Str2Hex(utils.Copy(rxBuf,index+9,4),4)
                COMMAND = utils.Str2Hex(utils.Copy(rxBuf,index+13,2),2)
                local DATAIndex = 15

                print("VER "..string.format("%02X",VER[1]))
                print("ADR "..string.format("%02X",ADR[1]))
                print("CID1 "..string.format("%02X",CID1[1]))
                print("CID2 "..string.format("%02X",CID2[1]))
                print("LENGTH "..string.format("%02X%02X",LENGTHTmp[1],LENGTHTmp[2]))
                print("COMMAND "..string.format("%02X",COMMAND[1]))

                --判断设备标识码和控制标识码
                if(CID1[1]==0x4A and CID2[1]==0x00) then
                    --获取第1组电池遥测数据
                    if(COMMAND[1] == 0x01) then
                        -------------------------------------------------------------------------------
                        local SOC = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        DATAIndex = DATAIndex + 4
                        -------------------------------------------------------------------------------
                        local TotalVol = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryCnt = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 1
                        -------------------------------------------------------------------------------
                        local BatteryVol01 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol02 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol03 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol04 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol05 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol06 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol07 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol08 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol09 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol10 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol11 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol12 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol13 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol14 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol15 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryVol16 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local Temperature = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryTemp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local MOSTemp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryTempCnt = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,1),1)
                        DATAIndex = DATAIndex + 1
                        -------------------------------------------------------------------------------
                        local BatteryTemp01 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryTemp02 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryTemp03 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local BatteryTemp04 = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local Current = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local SOH = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local UserCnt = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,1),1)
                        DATAIndex = DATAIndex + 1
                        -------------------------------------------------------------------------------
                        local FullCap = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local SurplusCap = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local CycleInde = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local VoltateState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        local VoltateStateB2 = utils.And(VoltateState,0x0004)
                        if(VoltateStateB2 == 0) then
                            
                        else 

                        end 
                        -------------------------------------------------------------------------------
                        local CurrentState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local TempState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local AlarmState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local FetState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local OverVoltateState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local UnderVoltateState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local HighVoltateState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local LowVoltateState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------
                        local EquilibriumState = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        DATAIndex = DATAIndex + 2
                        -------------------------------------------------------------------------------                  
                    end                 
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end


--utils.AnalysisRx(0x01,190)

return utils